package com.yanqu.road.server.cmd.marriage.cross;

import com.yanqu.road.pb.marriage.CrossMarriageProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.CrossMarriageGroupMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CROSS_MARRIAGE_APPLY_REFUSE, desc = "拒绝联姻请求")
public class CrossMarriageApplyRefuseCmd extends AbstractCommand {

    private String childUid;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossMarriageProto.CrossMarriageApplyRefuseReqMsg reqMsg = CrossMarriageProto.CrossMarriageApplyRefuseReqMsg.parseFrom(packet.getBytes());
        childUid = reqMsg.getChildUid();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(!CrossMarriageGroupMgr.canCrossMarriage(GameServer.getInstance().getServerId())){
            ret = GameErrorCode.E_CROSS_MARRIAGE_NO_OPEN;
            return;
        }
        CrossMarriageProto.CrossMarriageApplyRefuseReqMsg.Builder refuseMsg = CrossMarriageProto.CrossMarriageApplyRefuseReqMsg.newBuilder();
        refuseMsg.setChildUid(childUid);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_REFUSE_MARRIAGE, refuseMsg);
        GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(0 != ret) {
            CrossMarriageProto.CrossMarriageApplyRefuseRespMsg.Builder respMsg = CrossMarriageProto.CrossMarriageApplyRefuseRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_CROSS_MARRIAGE_APPLY_REFUSE, respMsg);
        }
    }
}
